// pages/addShop/addShop.js
import validate from '../../utils/validate.js'
const app = getApp()

function getNowDate2(){ //获取当前日期字符串返回
  let nowDate =  new Date();
  let month = nowDate.getMonth() + 1;
  if(month < 10){
    month = '0' + month;
  }
  let day = nowDate.getDate();
  if(day < 10) {
    day = '0' + day;
  }
  
  return nowDate.getFullYear() + '' + month + '' + day + '' + nowDate.getHours() + '' + nowDate.getMinutes() + '' + nowDate.getSeconds();
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_types : [] ,
    store_type_index:0,
    marker_orgs : [],
    marker_org_index : 0,
    terminal_types : [],
    terminal_type_index : 0,
    manage_brands : [],
    manage_brand_index : 0,
    manage_types : [],
    manage_type_index : 0,
    KAstore_types : [],
    KAstore_type_index : 0,
    defaultCode:getNowDate2(),

    index:0,
    org:['sf','sd'],
    imgList:[],
    itude:{},
    slatitude:'',
    slongitude:'',
    lMarkers:[]

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    app.api.getStoreType().then(res => {
        if(res.code == 200){
          that.setData({
            store_types : res.rows
          })
        }
        that.querystorebytype(this.data.store_types[0].id);
      })
    app.api.getMarkerOrg().then(res => {
      if(res.code == 200){
        that.setData({
          marker_orgs : res.rows
        })
      }
    })
    app.api.getTerminalType().then(res => {
      if(res.code == 200){
        that.setData({
          terminal_types : res.rows
        })
      }
    })
    app.api.getManageBrand().then(res => {
      if(res.code == 200){
        that.setData({
          manage_brands : res.rows
        })
      }
    })
    app.api.getManageType().then(res => {
      if(res.code == 200){
        that.setData({
          manage_types : res.rows
        })
      }
    })
    this.getMylocation();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  ChooseImage() {
    wx.chooseImage({
      count: 4 - this.data.imgList.length, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        this.setData({
          imgList: this.data.imgList.concat(res.tempFilePaths)
        })
      }
    });
  },
  openCanmera(){
    wx.navigateTo({
      url: '/pages/takePhoto/takePhoto'
    })
  },
  addImg(img){
    this.setData({
      imgList: this.data.imgList.concat(img)
    })
  },
  DelImg(index){
    this.data.imgList.splice(index,1);
    this.setData({
      imgList: this.data.imgList
    })
  },
  changeStoreType(event){
    this.setData({
      store_type_index : event.detail.value
    })
    this.querystorebytype(this.data.store_types[event.detail.value].id);
  },
   changeMarkerType(event){
    this.setData({
      marker_org_index : event.detail.value
    })
  },
  changeTerminalType(event){
    this.setData({
      terminal_type_index : event.detail.value
    })
  },
  changeManageBrand(event){
    this.setData({
      manage_brand_index : event.detail.value
    })
  },
  changeManageType(event){
    this.setData({
      manage_type_index : event.detail.value
    })
  },
  changeKAStoreType(event){
    this.setData({
      KAstore_type_index : event.detail.value
    })
  },
  querystorebytype(key){
    let that = this;
    app.api.querystorebytype(key).then(res => {
      if(res.code == 200){
        that.setData({
          KAstore_types : res.rows
        })
      }
    })
  },
  submit(event){
    let shop = {};
    let obj = event.detail.value;
    shop.storecode = obj.storecode;
    shop.storename = obj.storename;
    shop.storeintro = obj.storeintro;
    shop.storetype = this.data.store_types[this.data.store_type_index].id;
    shop.marketorg = this.data.marker_orgs[this.data.marker_org_index].id;
    shop.terlevel = this.data.terminal_types[this.data.terminal_type_index].id;
    shop.linkname = obj.linkname;
    shop.linkphone = obj.linkphone;
    shop.linkpost = obj.linkpost;
    shop.linkaddress = obj.linkaddress;
    shop.managebrand = this.data.manage_brands[this.data.manage_brand_index].id;
    shop.managetype = this.data.manage_types[this.data.manage_type_index].id;
    if( this.data.KAstore_types.length > 0){
      shop.refka = this.data.KAstore_types[this.data.KAstore_type_index].id;
    }
    shop.iskey = obj.iskey ? 'Y' : 'N'
    shop.itude = this.data.itude;
    console.log(event.detail.value)
    console.log(shop)
    if(this.validateSubmit(shop)){
      app.api.addstore(shop).then(res => {
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: '保存成功!',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }
      })
    }
  },
  selectPoint:function(){
    // wx.navigateTo({
    //   url: '../myMap/myMap?loc='+JSON.stringify(this.data.itude),
    // })
    wx.chooseLocation({
      latitude:this.data.itude.latitude,
      longitude:this.data.itude.longitude,
      success:res => {
        console.log(res)
        let location = {};
        location.latitude = res.latitude
        location.longitude = res.longitude;
        this.setData({
          itude : location
        })

      }
    });
  },
  clickMarker:function(event){
    let target =event.detail;
    target.id = 1;
    this.setData({
      lMarkers:[target]
    })
  },
  setLocation(obj){
    this.setData({
      itude:obj
    })
  },
  getMylocation(){
    wx.getLocation({
      type: 'gcj02',
      success:(res) =>{
        let obj = {latitude:res.latitude,longitude:res.longitude}
        this.setData({
          itude : obj
        })
      },
      fail : res => {
        if('getLocation:fail auth deny' == res.errMsg){
          console.log(res)
          wx.showModal({
            title: '提示',
            content: '获取用户授权失败',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else if('getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' == res.errMsg){
          wx.showModal({
            title: '提示',
            content: '请打开手机定位',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else{
          wx.showToast({
            title: res.errMsg,
          })
        }
      }
    })
  },
  validateSubmit(obj){
    let msg = ''
    if(validate.validatenull(obj.storecode)){
      msg = '终端客户编码';
    }else if(validate.validatenull(obj.storename)){
      msg = '终端名称';
    }else if(validate.validatenull(obj.storeintro)){
      msg = '终端简介';
    }else if(validate.validatenull(obj.linkname)){
      msg = '联系人姓名'
    }else if(validate.validatenull(obj.linkphone)){
      msg = '联系人手机';
    }else if(validate.validatenull(obj.linkpost)){
      msg = '联系人职位';
    }else if(validate.validatenull(obj.linkaddress)){
      msg = '店铺地址';
    }else{
      return true;
    }
    wx.showModal({
      title: '提示',
      content: msg + '不能为空',
      showCancel:false
    })
    return false;
  }
})